/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-06-26 14:12:15
 * @Description: 
 */

import * as API from '../../api'

class Chat {

    constructor(that) {
        this.component = that;
        this.userInfo = this.component.$store.state.user.userInfo;
        this.chatHandler();
        this.connect();
    }

    chatHandler() {
        let tthis = this;

        this.MessageErrorHandler = {
            proccess: function(obj, args) {
                console.log("MessageErrorHandler:", args);
                let code = args.code;
                //todo
                if (code == 4) {
                    tthis.component.$store.dispatch('user_loginOut');
                } else {
                    tthis.component.$store.dispatch('chat_messageErrorCode', code);
                }
            }
        };

        this.ConnentedHandler = {
            proccess: function(obj, args) {
                MiniIM.log(" Websoket connent successful!");
                MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.UNREAD, tthis.component.unreadHandler);
                MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.MESSAGE, tthis.component.MessageHandler);
                MiniIM.Action.messageMirror(true);
                MiniIM.Action.loadUnreadMessages();
                if (Config.CHAT_ROOM) {
                    MiniIM.Action.joinRoom(MessageName);
                }
                tthis.component.$root.$emit('chat_connented');
                //tthis.component.connectOK();
            }
        };
    }

    connect() {
        let tthis = this;
        if (MiniIM.getLoginName() && MiniIM.getLoginPassword()) {
            console.log('reconnect IM');
            MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.ERROR, this.MessageErrorHandler);
            MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.CONNECTED, this.ConnentedHandler);
            MiniIM.connect(MiniIM.getLoginName(), MiniIM.getLoginPassword());
        } else {
            API.get_sig({
                data: {},
                success: (response) => {
                    MiniIM.init('wss://www.' + MessageName + '.com/ws/', MessageName, response.session_id, {
                        'protocol': 'ws'
                    });
                    MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.ERROR, tthis.MessageErrorHandler);
                    MiniIM.Handler.addHandler(MiniIM.Handler.KEYS.CONNECTED, tthis.ConnentedHandler);
                    MiniIM.connect(tthis.createJid(), response.sig);
                },
                error: (response) => {
                    console.log('error', response);
                }
            });
        }
    }

    createJid() {
        var username = this.userInfo.account.username.toLowerCase(),
            rid = Unit.randomWord(false, 10),
            type = '';
        if (MessageName == 'masonsoft') {
            type = '-dev';
        } else if (MessageName == 'masontest') {
            type = '-test';
        }
        var jidOrHost = username + '__' + MessageName + '@jabber' + type + '.successfulmatch.com';
        //var jid = username + '__' + MessageName + '@jabber' + type + '.successfulmatch.com/' + rid;
        console.log(jidOrHost);
        return jidOrHost;
    }

}

export default Chat;
